/*
 * Copyright [2012] [Robert James Szabo]

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

package com.sababado.analytics;

import java.util.Map;
import java.util.TreeMap;

import android.content.Context;
import android.util.Log;

import com.flurry.android.FlurryAgent;


/**
 * This manager is designed specifically around Flurry analytics.
 * Visit <a href="http://www.flurry.com">http://www.flurry.com</a> to sign up and add the sdk to your project if you plan on using this class.
 * @author Robert J. Szabo
 * @since 08/27/2012
 * @version 1.0
 */
public class FlurryManager
{
	/**
	 * Specific event for Orientation when logging events
	 */
	public static final String EVENT_NAME_ORIENTATION = "Orientation";
	
	/**
	 * Constant used to not log any calls
	 */
	public static final int TRACKING_OFF = 0;
	
	/**
	 * Constant used to log all calls
	 */
	public static final int TRACKING_ON_ALL = 1;
	
	//Used to see which calls should be logged.
	private static int mTrackingLevel = TRACKING_ON_ALL;
	
	/**
	 * Set a log level so that the FlurryManager can determine which Flurry calls should be allowed.
	 * @param logLevel
	 */
	public static void setTrackingLevel(int logLevel)
	{
		if(logLevel != TRACKING_OFF && logLevel != TRACKING_ON_ALL)
			throw new RuntimeException("Inappropriate use of tracking level. Use a FlurryManager.TRACKING_* constant");
		mTrackingLevel = logLevel;
	}
	
	/**
	 * Start a session. Should be called in an onStart for any activity
	 * @param ctx
	 * @param apiKey
	 */
	public static void onStartSession(Context ctx, String apiKey, boolean reportLocation)
	{
		if(mTrackingLevel > 0)
		{
			FlurryAgent.setReportLocation(reportLocation);
			FlurryAgent.onStartSession(ctx, apiKey);
		}
	}
	
	/**
	 * End a session. This should be called in an onStop of an activity in which
	 * a session was started.
	 * @param ctx
	 */
	public static void onEndSession(Context ctx)
	{
		if(mTrackingLevel > 0)
		{
			FlurryAgent.onEndSession(ctx);
		}
	}
	
	/**
	 * End an event that is being timed.
	 * @param eventId
	 */
	public static void endTimedEvent(String eventId)
	{
		if(mTrackingLevel > 0)
		{
			FlurryAgent.endTimedEvent(eventId);
		}
	}
	
	/**
	 * Log an event with different parameters
	 * @param eventId Event Id
	 * @param isPortraitOrientation True if the device orientation is in portrait mode, false if in landscape
	 * @param extras A map of extra parameters (like current sport, team, etc)
	 * @param timed True if the the event is timed, in which case should be followed with a FlurryAgent.endTimedEvent call
	 */
	public static void logEvent(String eventId, boolean isPortraitOrientation, Map<String,String> extras, boolean timed)
	{
		if(mTrackingLevel > 0)
		{
			Log.d("FlurryManager","**Logging event: "+eventId);
			if(extras == null)
				extras = new TreeMap<String, String>();
			extras.put(EVENT_NAME_ORIENTATION, ( (isPortraitOrientation) ? ("Portrait") : ("Landscape")));
			if(timed)
				FlurryAgent.logEvent(eventId, extras, timed);
			else
				FlurryAgent.logEvent(eventId, extras);
		}
	}
	
	/**
	 * Log an event with different parameters
	 * @param eventId Event Id
	 * @param extras A map of extra parameters (like current sport, team, etc)
	 * @param timed True if the the event is timed, in which case should be followed with a FlurryAgent.endTimedEvent call
	 */
	public static void logEvent(String eventId, Map<String,String> extras, boolean timed)
	{
		if(mTrackingLevel > 0)
		{
			Log.d("FlurryManager","**Logging event: "+eventId);
			if(extras == null)
				extras = new TreeMap<String, String>();
			if(timed)
				FlurryAgent.logEvent(eventId, extras, timed);
			else
				FlurryAgent.logEvent(eventId, extras);
		}
	}
	
	
	/**
	 * Log an error
	 * @param eventId
	 * @param message
	 * @param errorClass
	 */
	public static void onError(String eventId, String message, String errorClass)
	{
		if(mTrackingLevel > 0)
		{
			Log.d("FlurryManager","**Logging error: "+eventId);
			FlurryAgent.onError(eventId, message, errorClass);
		}
	}
}
